package cn.iocoder.yudao.module.project.controller.admin.versioncontrol;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.project.controller.admin.versioncontrol.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.versioncontrol.VersionControlDO;
import cn.iocoder.yudao.module.project.service.versioncontrol.VersionControlService;

@Tag(name = "管理后台 - 版本控制")
@RestController
@RequestMapping("/project/version-control")
@Validated
public class VersionControlController {

    @Resource
    private VersionControlService versionControlService;

    @PostMapping("/create")
    @Operation(summary = "创建版本控制")
    // @PreAuthorize("@ss.hasPermission('project:version-control:create')")
    public CommonResult<Long> createVersionControl(@Valid @RequestBody VersionControlSaveReqVO createReqVO) {
        return success(versionControlService.createVersionControl(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新版本控制")
    // @PreAuthorize("@ss.hasPermission('project:version-control:update')")
    public CommonResult<Boolean> updateVersionControl(@Valid @RequestBody VersionControlSaveReqVO updateReqVO) {
        versionControlService.updateVersionControl(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除版本控制")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('project:version-control:delete')")
    public CommonResult<Boolean> deleteVersionControl(@RequestParam("id") Long id) {
        versionControlService.deleteVersionControl(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得版本控制")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('project:version-control:query')")
    public CommonResult<VersionControlRespVO> getVersionControl(@RequestParam("id") Long id) {
        VersionControlDO versionControl = versionControlService.getVersionControl(id);
        return success(BeanUtils.toBean(versionControl, VersionControlRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得版本控制分页")
    // @PreAuthorize("@ss.hasPermission('project:version-control:query')")
    public CommonResult<PageResult<VersionControlRespVO>> getVersionControlPage(@Valid VersionControlPageReqVO pageReqVO) {
        PageResult<VersionControlDO> pageResult = versionControlService.getVersionControlPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VersionControlRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出版本控制 Excel")
    // @PreAuthorize("@ss.hasPermission('project:version-control:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVersionControlExcel(@Valid VersionControlPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VersionControlDO> list = versionControlService.getVersionControlPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "版本控制.xls", "数据", VersionControlRespVO.class,
                        BeanUtils.toBean(list, VersionControlRespVO.class));
    }




    @GetMapping("/check")
    public CommonResult<VersionControlDO> checkVersion(@RequestParam String currentVersion) {
        VersionControlDO version = versionControlService.selectLastVersion();
        if (version == null) {
            return success(null);
        }
        // 比较版本号,如果最新版本号大于当前版本号，则返回最新版本号
        if (compareVersion(currentVersion, version.getVersion()) < 0) {
            return success(version);
        }

        return success(null);
    }

    // 版本号比较方法
    private int compareVersion(String v1, String v2) {
        String[] version1 = v1.split("\\.");
        String[] version2 = v2.split("\\.");

        int i = 0;
        while (i < version1.length && i < version2.length) {
            int num1 = Integer.parseInt(version1[i]);
            int num2 = Integer.parseInt(version2[i]);

            if (num1 < num2) return -1;
            else if (num1 > num2) return 1;
            i++;
        }

        if (version1.length < version2.length) return -1;
        if (version1.length > version2.length) return 1;
        return 0;
    }

}